<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <div class="search">
        <div class="searchcopy">
          <el-form-item label="图号:">
            <el-input v-model="queryParams.imageCode" placeholder="请输入图号" style="width: 240px"
                      @keyup.enter.native="handleQuery" @keyup.native="keyUp" minlength="1" maxlength="20"
                      show-word-limit filterable clearable></el-input>
          </el-form-item>
          <el-form-item label="产线名称:">
            <el-select v-model="queryParams.plName" placeholder="请输入产线名称" filterable clearable
                       style="width: 240px">
              <el-option v-for="item in lineName" :key="item.label" :label="item.label"
                         :value="item.label"/>
            </el-select>
          </el-form-item>
        </div>
        <div class="search-btn-style">
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </el-form>
    <div class="add-btn">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="small"
        @click="openDialog"
      >新增
      </el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-upload"
        id="btns-style"
        @click="handleimport()"
      >导入
      </el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-download"
        filterable
        id="btns-style"
        @click="handleExcel"
      >
        导出
      </el-button>
      <el-button
        type="danger"
        size="small"
        icon="el-icon-delete"
        filterable
        id="btns-style"
        @click="handledelTimeNodeList()"
      >删除
      </el-button>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </div>
    <el-table
      v-loading="loading"
      :data="stationList"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"></el-table-column>
      <el-table-column
        label="图号"
        align="center"
        prop="imageCode"
        min-width="220"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="产线"
        align="center"
        prop="plName"
        min-width="150"
        class="form-content-width"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="单根工时（分）"
        align="center"
        prop="workingHour"
        min-width="120"
        class="form-content-width"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="AGV单次下线运输量"
        align="center"
        prop="agvTransVolume"
        min-width="140"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="下料滞后时间（分）"
        sortable
        align="center"
        prop="blankingLagTime"
        :show-overflow-tooltip="true"
        min-width="170"
      >
      </el-table-column>
      <el-table-column
        label="起点站点"
        sortable
        align="center"
        prop="startStationName"
        :show-overflow-tooltip="true"
        min-width="160"
      >
      </el-table-column>
      <el-table-column
        label="终点站点"
        sortable
        align="center"
        prop="endStationName"
        :show-overflow-tooltip="true"
        min-width="160"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="mark"
        min-width="80"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建人"
        align="center"
        prop="creator"
        min-width="80"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="更新人"
        align="center"
        prop="updater"
        min-width="80"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="更新时间"
        sortable
        align="center"
        prop="updateTime"
        :show-overflow-tooltip="true"
        min-width="160"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="60"
      >
        <template v-slot="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <uploadExcel ref="tochannel" :title="tochanneldata.title" @refresh="refresh"></uploadExcel>
    <addDialog ref="addTochannel" :title="addTochannelData.title" @refresh="refresh"/>
    <editDialog ref="editTochannel" :title="editTochannelData.title" :formData="editTochannelData.formData"
                @refresh="refresh"/>
  </div>
</template>
<script>
import {
  getPreparationList,
  delPreparationList,
  getExcel,
  downloadOfflineExcel,
  getlinename,
} from "@/api/BasicConfiguration/offline";
import {listData} from "@/api/system/dict/data";
import uploadExcel from '@/views/BasicConfiguration/offline/uploadExcel.vue'
import addDialog from "@/views/BasicConfiguration/offline/addOfflineDialog.vue";
import editDialog from "@/views/BasicConfiguration/offline/editOfflineDialog.vue";

export default {
  components: {editDialog, addDialog, uploadExcel},
  data() {
    var validatePass = (rule, value, callback) => {
      //当输入的数据有值，值是非空，输入的数据不是数字类型时 进行提示
      if (value && value < 0 && Number.isInteger(value) || (value && value !== "" && typeof value !== 'number')) {
        callback(new Error("下线滞后时间（分）必须为数值类型且不能为负数"));
      } else {
        callback();
      }
    };
    var validatePassAgv = (rule, value, callback) => {
      if (!value || value <= 0 || !Number.isInteger(value)) {
        callback(new Error("单次AGV下线运输量必须为正整数"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false, //是否展示弹框
      dialogFormVisible: false,
      lineName: null,
      multipleSelection: [],
      value: "",
      // 弹出层标题
      dialogTitle: "新增",
      // 遮罩层 查询
      loading: true,
      // 总条数
      total: 0,
      // 显示搜索条件
      showSearch: true,
      // 列表
      stationList: [],
      //起点站点
      starStation: {
        iseb: 1,
      },
      startdata: null,
      // 站点数据
      position: 1,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        imageCode: null,
        plName: null,
      },
      // 导入的参数
      tochanneldata: {
        title: "",
      },
      form: {
        workingHour: null,
      },
      // 新增的参数
      addTochannelData: {
        title: ""
      },
      // 编辑的参数
      editTochannelData: {
        title: "",
        formData: null,
      },
      // 下线图号的终点站点
      preparationend: [],
      // 库位的 库位类型
      positiontype: [],
    };
  },
  mounted() {
    this.getList();
    // 初始化库位类型list
    this.getTypeList();
  },
  methods: {
    // 初始化库位类型list
    getTypeList() {
      const data = {
        dictType: "position_type",
      };
      // 调用根据数据字典类型查询居委类型列表
      listData(data)
        .then((res) => {
          if (res.data && res.data.list) {
            this.positiontype = res.data.list;
          }
        })
        .catch(() => {
        });
    },
    // 子组件刷新列表
    refresh() {
      this.getList();
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 单根工时保留小数点后一位
    handleChange(value) {
      this.form.workingHour = value.toFixed(1);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.imageCode = null;
      this.queryParams.plName = null;
      this.handleQuery();
    },
    /** 获取列表 */
    getList() {
      this.loading = true;
      getPreparationList(this.queryParams).then((response) => {
        const newList = response.data.list.map(item => {
          return item;
        })
        this.stationList = newList;
        this.preparationLeadTime = response.data.preparationLeadTime;
        this.stationList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
        this.getplannamedata();
      });
    },
    // 获取当天的产线名
    getplannamedata() {
      getlinename().then(res => {
        this.lineName = res.data.map(item => {
          return {'label': item}
        })
      })
    },
    // 新增
    openDialog() {
      this.addTochannelData.title = "新增";
      this.$refs.addTochannel.dialogFormVisible = true;
    },
    // 编辑
    handleEdit(row) {
      this.editTochannelData.title = "编辑";
      this.$refs.editTochannel.dialogFormVisible = true;
      this.editTochannelData.formData = JSON.parse(JSON.stringify(row));
      this.editTochannelData.formData.endStationId = String(this.editTochannelData.formData.endStationId)
    },
    //批量删除
    handledelTimeNodeList() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择要删除的数据");
      } else {
        let ids = this.multipleSelection.map((item) => {
          return item.id;
        });
        this.$modal
          .confirm("确定要删除吗？")
          .then(function () {
            return delPreparationList(ids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {
          });
      }
    },
    // 导入
    handleimport() {
      this.tochanneldata.title = "导入下线图号配置";
      this.$refs.tochannel.upload.open = true;
    },
    // 下载模板
    downloadTemplate() {
      downloadOfflineExcel()
        .then((response) => {
          this.$download.excel(response, "下线图号配置模板.xls");
          this.$message.success("模版下载成功");
        })
        .catch(() => {
          this.$message.error("模版下载失败");
        });
    },
    // 导出
    handleExcel() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning('请选择要导出的下线图号配置')
      } else {
        const ids = this.multipleSelection.map(item => {
          return item.id
        })
        this.$modal.confirm('是否确认导出选中下线图号配置?').then(() => {
          this.exportLoading = true;
          return getExcel(ids);
        }).then(response => {
          this.$download.excel(response, '下线图号配置.xls');
          this.exportLoading = false;
        }).catch(() => {
        });
      }
    },
  },
};
</script>
<style scoped>
.searchcopy {
  background-color: #fff;
  padding: 0 20px;
  padding-top: 17px;
  border-radius: 4px;
  display: inline-block;
}

.search > * {
  padding: 0;
  margin-right: 30px !important;
}

.dialog-list-width {
  width: 155px;
}

.el-form-item--medium .el-form-item__label {
  width: 155px;
}
</style>
